using System;
using System.Data;
using Spread.Common.DataStore;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Data;
using FarPoint.Win.Spread.DrawingSpace;
using FarPoint.Win.Spread.Model;

namespace Spread.FloatingObjects.Features
{
    public partial class CameraShapes : Spread.Common.Features.Sample.BaseSampleControl
    {
        private AccessDataSource _dataSource;

        public CameraShapes()
        {
            InitializeComponent();

            Name = "Camera Shapes";
            _dataSource = new DataService().AccessDataSource;

            InitSpreadSheet(fpSpread.ActiveSheet);

            txtRow.TextChanged += TextBox_TextChanged;
            txtColumn.TextChanged += TextBox_TextChanged;
            fpSpread.SelectionChanged += FpSpread_SelectionChanged;
            btnAddShape.Click += BtnAddShape_Click;

            fpSpread.ShapeActivated += FpSpread_ShapeActivated;
        }

        private void InitSpreadSheet(SheetView sheet)
        {
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 20);

            DataTable table = _dataSource.GetDataFrom("fpWine.mdb", "select ProductID, VendorID, Product, Description," +
                "Price, QtyInStock from Products").Tables[0];
            SpreadDataBindingAdapter adapter = new SpreadDataBindingAdapter();
            adapter.DataAutoCellTypes = true;
            adapter.DataSource = table;
            adapter.MapperInfo = new MapperInfo(0, 0, table.Rows.Count, table.Columns.Count);
            adapter.Spread = fpSpread;
            adapter.SheetName = fpSpread.ActiveSheet.SheetName;
            adapter.FillSpreadDataByDataSource();

            for (int col = 0; col < table.Columns.Count; ++col)
                sheet.Columns[col].Label = table.Columns[col].ColumnName;

            sheet.Columns[2].Width = 120;
            sheet.Columns[3].Width = 300;
            sheet.Columns[4].Width = 80;
            sheet.Columns[5].Width = 100;
        }

        private bool ValidateInput()
        {
            if (!fpSpread.ActiveSheet.GetSelection(0).IsValidRange(fpSpread.ActiveSheet))
                return false;

            int val = -1;
            if (!int.TryParse(txtRow.Text, out val) || !int.TryParse(txtColumn.Text, out val))
                return false;

            return true;
        }

        #region Event handlers
        private void FpSpread_ShapeActivated(object sender, EventArgs e)
        {
            PSShape shape = (fpSpread.ActiveWindowlessObject as PSShape);
            shape.ForeColor = System.Drawing.Color.Beige;
        }

        private void BtnAddShape_Click(object sender, EventArgs e)
        {
            CellRange cellRange = fpSpread.ActiveSheet.GetSelection(0);
            SpreadCameraShape shape = new SpreadCameraShape(fpSpread.ActiveSheet, cellRange);

            int row = int.Parse(txtRow.Text);
            int col = int.Parse(txtColumn.Text);
            fpSpread.ActiveSheet.AddShape(shape, row, col);
        }

        private void FpSpread_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnAddShape.Enabled = ValidateInput();
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            btnAddShape.Enabled = ValidateInput();
        }
        #endregion
    }
}


//
//CameraShapes.Designer.cs
//


namespace Spread.FloatingObjects.Features
{
    partial class CameraShapes
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.txtRow = new System.Windows.Forms.TextBox();
            this.txtColumn = new System.Windows.Forms.TextBox();
            this.btnAddShape = new System.Windows.Forms.Button();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.btnAddShape);
            this.pnlControls.Controls.Add(this.txtColumn);
            this.pnlControls.Controls.Add(this.txtRow);
            this.pnlControls.Controls.Add(this.label3);
            this.pnlControls.Controls.Add(this.label2);
            this.pnlControls.Controls.Add(this.label1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Font = new System.Drawing.Font("Segoe UI", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(157, 42);
            this.label1.TabIndex = 0;
            this.label1.Text = "Select a range of cells and set destination position for the Camera Shape";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(5, 57);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Row:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(5, 83);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(45, 13);
            this.label3.TabIndex = 2;
            this.label3.Text = "Column:";
            // 
            // txtRow
            // 
            this.txtRow.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtRow.Location = new System.Drawing.Point(59, 54);
            this.txtRow.Name = "txtRow";
            this.txtRow.Size = new System.Drawing.Size(86, 20);
            this.txtRow.TabIndex = 3;
            // 
            // txtColumn
            // 
            this.txtColumn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtColumn.Location = new System.Drawing.Point(59, 80);
            this.txtColumn.Name = "txtColumn";
            this.txtColumn.Size = new System.Drawing.Size(86, 20);
            this.txtColumn.TabIndex = 4;
            // 
            // btnAddShape
            // 
            this.btnAddShape.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnAddShape.Enabled = false;
            this.btnAddShape.Location = new System.Drawing.Point(22, 113);
            this.btnAddShape.Name = "btnAddShape";
            this.btnAddShape.Size = new System.Drawing.Size(118, 26);
            this.btnAddShape.TabIndex = 5;
            this.btnAddShape.Text = "Add Camera Shape";
            this.btnAddShape.UseVisualStyleBackColor = true;
            // 
            // CameraShapes
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "CameraShapes";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            this.pnlControls.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnAddShape;
        private System.Windows.Forms.TextBox txtColumn;
        private System.Windows.Forms.TextBox txtRow;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
    }
}
